package org.example.rzfx.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import org.example.rzfx.dto.Result;
import org.example.rzfx.entity.LogSource;
import org.example.rzfx.service.LogSourceService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * 日志采集源控制器
 */
@RestController
@RequestMapping("/api/log-source")
@CrossOrigin
public class LogSourceController {
    
    @Resource
    private LogSourceService logSourceService;
    
    /**
     * 查询所有采集源
     */
    @GetMapping("/list")
    public Result<List<LogSource>> listLogSources() {
        return Result.success(logSourceService.list());
    }
    
    /**
     * 获取采集源详情
     */
    @GetMapping("/{id}")
    public Result<LogSource> getLogSource(@PathVariable Long id) {
        LogSource logSource = logSourceService.getById(id);
        if (logSource == null) {
            return Result.error("采集源不存在");
        }
        return Result.success(logSource);
    }
    
    /**
     * 创建采集源（仅管理员）
     */
    @PostMapping("/create")
    @SaCheckRole("1")
    public Result<Void> createLogSource(@RequestBody LogSource logSource) {
        logSourceService.save(logSource);
        return Result.success("创建成功", null);
    }
    
    /**
     * 更新采集源（仅管理员）
     */
    @PutMapping("/update")
    @SaCheckRole("1")
    public Result<Void> updateLogSource(@RequestBody LogSource logSource) {
        logSourceService.updateById(logSource);
        return Result.success("更新成功", null);
    }
    
    /**
     * 删除采集源（仅管理员）
     */
    @DeleteMapping("/{id}")
    @SaCheckRole("1")
    public Result<Void> deleteLogSource(@PathVariable Long id) {
        logSourceService.removeById(id);
        return Result.success("删除成功", null);
    }
    
    /**
     * 启用/禁用采集源（仅管理员）
     */
    @PutMapping("/status/{id}/{status}")
    @SaCheckRole("1")
    public Result<Void> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        LogSource logSource = logSourceService.getById(id);
        if (logSource == null) {
            return Result.error("采集源不存在");
        }
        logSource.setStatus(status);
        logSourceService.updateById(logSource);
        return Result.success("更新成功", null);
    }
}

